package com.ait.edu.controller;



import com.ait.commonutils.Msg;
import com.ait.edu.entity.Teacher;
import com.ait.edu.entity.vo.TeacherQuery;
import com.ait.edu.service.TeacherService;
import com.ait.edu.service.impl.TeacherServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-06
 */
@RestController
@RequestMapping("/eduservice/teacher")
public class TeacherController {


    @Autowired
    private TeacherService teacherService;

    @ApiOperation("所有讲师")
    @GetMapping("getAllTeacher")
    public Msg getAll(){
        List<Teacher> list = teacherService.list(null);
        if (list != null)return Msg.success().data("teacherList",list);
        return Msg.fail();
    }

    @ApiOperation("ID讲师")
    @GetMapping("/c")
    public Msg getId(@RequestParam("id") String id){
        Teacher teacher = teacherService.getById(id);
        System.out.println(teacher);
        if(teacher != null){
            return Msg.success().data("teacher",teacher);
        }
        return Msg.fail();
    }
    @ApiOperation("逻辑删除讲师")
    @DeleteMapping("{id}")
    public Msg eduDel(@PathVariable String id){

        boolean b = teacherService.removeById(id);
        if (b){
           return Msg.success();
        }
        return Msg.fail();
    }




    @ApiOperation("分页")
    @GetMapping("pageTeacher")
    public Msg edulimit(Integer currnet,Integer limit){
        System.out.println(currnet+"-->"+ limit);
        Page<Teacher> page = new Page<>(currnet,limit);
        IPage<Teacher> page1 = teacherService.page(page, null);
       if(page1 != null ){
           long total = page.getTotal();
           List<Teacher> records = page.getRecords();
           return Msg.success().data("total",total).data("row",records);
       }
        return Msg.fail();

    }


    @ApiOperation("分页查询")
    @PostMapping("pageTeacherCondition")
        public Msg eduquery(  Integer currnet, Integer limit, @RequestBody(required = false) TeacherQuery teacherQuery){
        System.out.println(currnet+"-->"+limit+"-->"+teacherQuery);
        Page<Teacher> page = new Page<>(currnet,limit);
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();

        if (teacherQuery != null){
            String name = teacherQuery.getName();
            String begin = teacherQuery.getBegin();
            Integer level = teacherQuery.getLevel();
            String end = teacherQuery.getEnd();

            if(!StringUtils.isEmpty(name)){
                wrapper.like("name",name);
            }

            if(!StringUtils.isEmpty(level) ){
                wrapper.eq("level",level);
            }
            if(!StringUtils.isEmpty(begin)){
                wrapper.ge("gmt_create",begin);
            }
            if(!StringUtils.isEmpty(end)){
                wrapper.le("gmt_modified",end);
            }
        }

        IPage<Teacher> page1 = teacherService.page(page, wrapper);

        if(page1 != null ){
            long total = page.getTotal();
            List<Teacher> records = page.getRecords();
            return Msg.success().data("total",total).data("row",records);
        }
        return Msg.fail();
    }


    @ApiOperation("添加讲师")
    @PostMapping("addTeacher")
    public Msg addTeacher (@RequestBody Teacher teacher){
        boolean save = teacherService.save(teacher);
        if (save){
            return Msg.success();
        }
        return Msg.fail();
    }


    @ApiOperation("修改讲师")
    @PutMapping("modifyTeacher")
    public Msg modifyTeacher(@RequestBody Teacher teacher){
        boolean b = teacherService.updateById(teacher);
        if(b){return Msg.success();}
        return Msg.fail();
    }
}

